//$URL: http://dasding.googlecode.com/svn/trunk/data/dao/mongoDB/src/main/java/de/piratech/dasding/data/database/mongo/AbstractMongoDB.java $
//$Id: AbstractMongoDB.java 118 2013-04-01 01:11:50Z krieg.maria@gmail.com $
/*
 * Copyright 2012 Piratech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.piratech.dasding.data.database.mongo;

import java.util.ArrayList;
import java.util.List;

import net.vz.mongodb.jackson.DBRef;
import net.vz.mongodb.jackson.JacksonDBCollection;

import org.bson.types.ObjectId;

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import de.piratech.dasding.data.AbstractDataObject;
import de.piratech.dasding.data.database.DatabaseException;
import de.piratech.dasding.data.database.mongo.data.AbstractMongoDataObject;

/**
 * 
 * @author deveth0
 */
public abstract class AbstractMongoDB<T extends AbstractMongoDataObject<K>, K extends AbstractDataObject> {

  public enum COLLECTIONS {

    ACTIONCATEGORY_COLLECTION("actioncategories"), EVENT_COLLECTION("events"), EVENTCATEGORY_COLLECTION(
        "eventscategories"), USER_COLLECTION("users"), ACTION_COLLECTION(
        "action"), LOCATION_COLLECTION("locations"), ACTIONAPPLICATION_COLLECTION(
        "actionapplications");
    private String mName;

    private COLLECTIONS(String pName) {
      mName = pName;
    }

    public String getName() {
      return mName;
    }
  }

  private JacksonDBCollection<T, String> dbCollection;
  protected DB db;

  public AbstractMongoDB(DB pDB, Class<T> pClass, COLLECTIONS pCollection) {
    db = pDB;
    dbCollection = JacksonDBCollection.wrap(
        pDB.getCollection(pCollection.getName()), pClass, String.class);
  }

  protected JacksonDBCollection<T, String> getDBCollection() {
    return dbCollection;
  }

  protected void checkObjectExists(String collection, AbstractDataObject object)
      throws DatabaseException {
    if (object == null) {
      throw new DatabaseException("object is null");
    }
    DBCollection coll = db.getCollection(collection);
    DBObject searchById = new BasicDBObject("_id", new ObjectId(object.getId()));
    DBObject found = coll.findOne(searchById);
    if (found == null) {
      throw new DatabaseException("object does not exist");
    }
  }

  protected <S extends AbstractMongoDataObject<?>> List<T> findByObjectReference(
      String field, String collectionName, String searchId, Class<S> searchClass) {
    List<T> array = getDBCollection().find()
        .is(field, new DBRef<S, String>(searchId, collectionName)).toArray();
    return array;
  }

  protected <S extends AbstractMongoDataObject<?>> List<T> findByObjectRefInArray(
      String field, String objectId, Class<S> searchClass) {
    BasicDBObject query = new BasicDBObject();
    query.put("$elemMatch", new DBRef<S, String>(objectId, searchClass));
    BasicDBObject foo = new BasicDBObject();
    foo.put(field, query);
    List<T> array = getDBCollection().find(foo).toArray();
    return array;
  }

  protected List<K> getDataObjectList(List<T> mongoList) {
    List<K> list = new ArrayList<K>();
    for (T mongoAction : mongoList) {
      list.add(mongoAction.getDataObject());
    }
    return list;
  }
}
